package com.ny.controller;

import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/devices")
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    /**
     * 查询所有设备信息
     * GET /devices
     */
    @GetMapping
    public Result getAllDevices() {
        try {
            List<Device> devices = deviceService.getAllDevices();
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据设备名称查询设备信息
     * GET /devices/search?name={deviceName}
     */
    @GetMapping("/search")
    public Result getDevicesByName(@RequestParam(required = false) String name) {
        try {
            List<Device> devices = deviceService.getDevicesByName(name);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败: " + e.getMessage());
        }
    }
}